<template>
  <div class="hr-list">
    <h2 class="page-title">招聘人员</h2>
    
    <!-- HR列表 -->
    <div class="hr-table">
      <el-table :data="hrList" style="width: 100%" v-loading="loading">
        <el-table-column label="头像" width="80">
          <template #default="scope">
            <el-avatar 
              :size="50" 
              :src="scope.row.face" 
              :alt="scope.row.realName"
            />
          </template>
        </el-table-column>
        <el-table-column prop="realName" label="姓名" width="120" />
        <el-table-column prop="nickName" label="昵称" width="120" />
        <el-table-column prop="position" label="职位" width="150" />
        <el-table-column prop="hrSignature" label="个人签名" min-width="200" />
        <el-table-column label="标签" min-width="300">
          <template #default="scope">
            <div class="tags-container">
              <el-tag 
                v-for="tag in getHrTags(scope.row.hrTags)" 
                :key="tag"
                size="small"
                class="tag-item"
              >
                {{ tag }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="绑定时间" width="180">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.pageNo"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getHrList } from '@/api/company'

const loading = ref(false)
const hrList = ref([])

// 分页配置
const pagination = ref({
  pageNo: 1,
  pageSize: 10,
  total: 0
})

// 获取HR列表
const fetchHrList = async () => {
  loading.value = true
  try {
    const response = await getHrList({
      pageNo: pagination.value.pageNo,
      pageSize: pagination.value.pageSize
    })
    
    if (response.list) {
      hrList.value = response.list
      // 这里假设接口返回了总数，如果没有需要调整
      pagination.value.total = response.total || response.list.length
    }
  } catch (error) {
    console.error('获取HR列表失败:', error)
    ElMessage.error('获取HR列表失败')
  } finally {
    loading.value = false
  }
}

// 处理标签分割
const getHrTags = (tags) => {
  if (!tags) return []
  return tags.split(',').filter(tag => tag.trim())
}

// 格式化时间
const formatTime = (timeStr) => {
  if (!timeStr) return ''
  const date = new Date(timeStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 处理分页大小变化
const handleSizeChange = (size) => {
  pagination.value.pageSize = size
  pagination.value.pageNo = 1
  fetchHrList()
}

// 处理当前页变化
const handleCurrentChange = (page) => {
  pagination.value.pageNo = page
  fetchHrList()
}

onMounted(() => {
  fetchHrList()
})
</script>

<style scoped>
.hr-list {
  padding: 2rem;
}

.page-title {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #2d3748;
  font-size: 1.6rem;
  font-weight: 500;
}

.hr-table {
  background: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border: 1px solid #e2e8f0;
  overflow: hidden;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag-item {
  margin: 0;
}

.pagination-container {
  padding: 1rem;
  display: flex;
  justify-content: center;
  background: white;
}
</style>
